package com.leyou.user.controller;

import com.leyou.user.dto.UserDTO;
import com.leyou.user.feignClient.UserClient;
import com.leyou.user.pojo.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 检验数据是否可用
     */
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUserData(@PathVariable("data") String data, @PathVariable("type") Integer type) {
        return ResponseEntity.ok(userService.checkUserData(data, type));
    }

    /**
     * 用户注册
     */
    @PostMapping("register")
    public ResponseEntity<Void> register(@Valid User user, @RequestParam("code") String code) {
        userService.register(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 用户查询
     */
    @GetMapping("query")
    public ResponseEntity<UserDTO> queryUserByNameAndPwd(@RequestParam("username")String username, @RequestParam("password")String password) {
        return ResponseEntity.ok(userService.queryUserByNameAndPwd(username, password));
    }
}
